﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CameraMove : MonoBehaviour
{
    Transform CamerDemo;

    //旋转变量
    float rotateY;
    float initialY;

   

    public float Camerdis;
    public Vector3 rayPoint;
    public Vector3 offet;
    float newDis;
    float oldDis;
    private Vector3 point;

    private int isforward;//标记摄像机的移动方向

    private float eulerAngles_x;
    private float eulerAngles_y;
    private float distancePoint;//绕点旋转距离

    public Texture2D ZoomView, PanView, OrbitView, FPSView;
    public Text PO;

    //public float rightPos;
    //public float leftPos;
    //public float upPos;
    //public float downPos;
    public Camera cam;
    Vector3 nowMousePos;
    public bool UIclipMove = false;//是否在ui上限制移动
  //  public PostProcessingProfile profile;

    bool LookFps=false;
    [HideInInspector]
    public float xMax=1000;
    [HideInInspector]
    public float xMin = -1000;
    [HideInInspector]
    public float yMax = 1000;
    [HideInInspector]
    public float yMin = -1000;
    [HideInInspector]
    public float hMax = 1000;

    public float hMin = -1000;
    // Use this for initialization
    void Start()
    {
        Camerdis = 1;
        RayPoint();
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    }
 
    // Update is called once per frame
    void Update()
    {
       // Debug.Log(UIclipMove);
        if (UIclipMove)
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }
        }
 
      // Debug.Log(Camera.main.ScreenToViewportPoint(Input.mousePosition).x+","+ Camera.main.ScreenToViewportPoint(Input.mousePosition).y); 
  
  
            //if (EventSystem.current.IsPointerOverGameObject())
            //{
            //    return;
            //}
            //if (Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(2))
            ////光标重置
            //{
            //    Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
            //}

            if (Input.GetMouseButton(0))
            //按住左键平移
            {
            
                CamerDemo = Camera.main.transform;
               // Cursor.SetCursor(PanView, Vector2.zero, CursorMode.Auto);
                Camerdis = (float)Math.Round(Vector3.Distance(Camera.main.transform.position, rayPoint), 2);
            if (Camerdis<1)
            {
                Camerdis = 1;
            }
                Translation(0.03f);
               // RangContrel();
                Camera.main.transform.position = CamerDemo.position;
                Camera.main.transform.eulerAngles = CamerDemo.eulerAngles;
            }
            if (Input.GetMouseButtonDown(0))
            {
                nowMousePos = Input.mousePosition;
                offet = Camera.main.transform.position - rayPoint;
            }
            if (Input.GetMouseButtonUp(0))
            {
                float mouseMoveV = Math.Abs(Vector3.Distance(nowMousePos, Input.mousePosition));
              //  Debug.Log(mouseMoveV);
                if (mouseMoveV > 20)
                {
                    rayPoint = Camera.main.transform.position - offet;
                }
             
                offet = Vector3.zero;
            }
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                CamerDemo = Camera.main.transform;
                Camerdis = (float)Math.Round(Vector3.Distance(transform.position, rayPoint), 2);
            if (Camerdis < 1)
            {
                Camerdis = 1;
            }
            CamerDemo.Translate(Vector3.back * Camerdis * 0.1f);
                RangContrel();
                Camera.main.transform.position = CamerDemo.position;
                Camera.main.transform.eulerAngles = CamerDemo.eulerAngles;
            }
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                CamerDemo = Camera.main.transform;
                // RayPoint();
                Camerdis = (float)Math.Round(Vector3.Distance(transform.position, rayPoint), 2);
            if (Camerdis < 1)
            {
                Camerdis = 1;
            }
            CamerDemo.Translate(Vector3.forward * Camerdis * 0.1f);
                RangContrel();
                Camera.main.transform.position = CamerDemo.position;
                Camera.main.transform.eulerAngles = CamerDemo.eulerAngles;

            }
            if (Input.GetMouseButtonDown(1)|| Input.GetMouseButtonDown(0))
            {
                CamerDemo = Camera.main.transform;
               // RayPoint();
            }
            if (Input.GetMouseButton(1))
            //右键旋转
            {
                CamerDemo = Camera.main.transform;
               // Cursor.SetCursor(OrbitView, Vector2.zero, CursorMode.Auto);
                RotatePoint();
                RangContrel();
                Camera.main.transform.position = CamerDemo.position;
                Camera.main.transform.eulerAngles = CamerDemo.eulerAngles;
            }
           // QualitySetForShadow();
          //  BlurEffect();

            //   KeyTranslation();
        

        if (Input.GetKeyDown(KeyCode.F10))
        {
            if (LookFps == false)
            {
                LookFps = true;
            }
            else
            {
                LookFps = false;
            }
        }
        if (LookFps)
        {
            lookFPS();
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            ColseFullScreen();
        }
    }
    public void UIclipMoveEnter()
    {
        UIclipMove = true;
    }
    public void UIclipMoveEixt()
    {
        UIclipMove = false;
    }
    void Translation(float sheep)
    //平移控制
    {
        float moveX = Input.GetAxis("Mouse X") ;
        float moveY = Input.GetAxis("Mouse Y") ;
        //自身坐标的z轴投影到世界坐标的z轴，用自身坐标的y轴和z轴的值乘 自身的相对欧拉角的x的三角函数。
        float tranY = moveY * (float)Math.Sin(Math.Round(CamerDemo.localRotation.eulerAngles.x, 2) * Math.PI / 180.0);
        float tranZ = moveY * (float)Math.Cos(Math.Round(CamerDemo.localRotation.eulerAngles.x, 2) * Math.PI / 180.0);
        CamerDemo.Translate(new Vector3(-moveX, -tranY, -tranZ) * Camerdis* sheep, Space.Self);
   
    }
    
    

    void Rotation()
    //旋转控制
    {
        initialY = CamerDemo.localRotation.eulerAngles.y;
        rotateY = 0;
        rotateY += Input.GetAxis("Mouse X") * 2f;
        var rotation = Quaternion.Euler(CamerDemo.localRotation.eulerAngles.x, rotateY + initialY, 0);
        CamerDemo.rotation = rotation;

    }
    void RayPoint()
    //射线得到碰撞点,针对绕点旋转
    {
        Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));//射线  

        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))//发射射线(射线，射线碰撞信息，射线长度，射线会检测的层级)  
        {
            rayPoint = new Vector3(hit.point.x, hit.point.y, hit.point.z);
         //   Camerdis = (float)Math.Round(cam.transform.position.y- hit.point.y, 2)+10;
        }
        else
        {
            rayPoint = transform.forward * 800 + transform.position;//摄像机前方 800 点
           // Camerdis = (float)Math.Round(cam.transform.position.y, 2) ;
        }
  
    }
    void RotatePoint()
    //绕点旋转
    {
       
        // Euler();
        Vector3 eulerAngles = CamerDemo.eulerAngles;
        this.eulerAngles_x = eulerAngles.y;
        this.eulerAngles_y = eulerAngles.x;
        //Debug.Log((Input.GetAxis("Mouse Y")));

        distancePoint = Vector3.Distance(transform.position, rayPoint);
        this.eulerAngles_x += (Input.GetAxis("Mouse X")) ;
        this.eulerAngles_y -= (Input.GetAxis("Mouse Y")) ;

        if (eulerAngles_y > 80)
        {
            this.eulerAngles_y = 80;
        }
        else if (eulerAngles_y < 1)
        {
            this.eulerAngles_y = 1;
        }
        //  Debug.Log(this.eulerAngles_y);
        Quaternion quaternion = Quaternion.Euler(this.eulerAngles_y, this.eulerAngles_x, (float)0);
        Vector3 vector = ((Vector3)(quaternion * new Vector3((float)0, (float)0, -distancePoint))) + rayPoint;

        CamerDemo.rotation = quaternion;
        CamerDemo.position = vector;
    
    }
    void Euler()
    //当前物体的欧拉角
    {
        Vector3 eulerAngles = CamerDemo.eulerAngles;
       this.eulerAngles_x = eulerAngles.y;
        this.eulerAngles_y = eulerAngles.x;
        if (eulerAngles.x>30)
        {
            this.eulerAngles_y = 30;
        }
        else if (eulerAngles.x <10)
        {
            this.eulerAngles_y =10;
        }
    }
    /// <summary>
    /// 键盘控制
    /// </summary>
    void KeyTranslation()
    {
        float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * 10;
        float moveY = Input.GetAxis("Vertical") * Time.deltaTime * 10;
        if (moveX == 0 && moveY == 0)
        {
            return;
        }
        //自身坐标的z轴投影到世界坐标的z轴，用自身坐标的y轴和z轴的值乘 自身的相对欧拉角的x的三角函数。
        float tranY = moveY * (float)Math.Sin(Math.Round(CamerDemo.localRotation.eulerAngles.x, 2) * Math.PI / 180.0);
        float tranZ = moveY * (float)Math.Cos(Math.Round(CamerDemo.localRotation.eulerAngles.x, 2) * Math.PI / 180.0);
        CamerDemo.Translate(new Vector3(moveX, tranY, tranZ) * Camerdis * 0.1f, Space.Self);
    }


    private double lastInterval;
    private int frames = 0;
    private float fps;

    public void lookFPS()
    {
        ++frames;
        float timeNow = Time.realtimeSinceStartup;
        if (timeNow > lastInterval + 0.5F)
        {
            fps = (float)(frames / (timeNow - lastInterval))+5f;
            frames = 0;
            lastInterval = timeNow;
        }
    }
    void OnGUI()
    {
        if (LookFps)
        {
            GUILayout.Label(" " + fps.ToString("f2"));
        }
        else
        {
            GUILayout.Label(" ");
        }
    }
    /// <summary>
    /// 阴影距离控制
    /// </summary>
    public void QualitySetForShadow()
    {
        //float dis = Vector3.Distance(Camera.main.transform.position, Vector3.zero);
        //float H = Camera.main.transform.position.y;
        //if (dis < 10)
        //{
        //    QualitySettings.shadowDistance = 200;
        //}
        //else
        //{
        //  QualitySettings.shadowDistance = dis *1.20f;
        //if (H * 2 < QualitySettings.shadowDistance)
        //{
       // QualitySettings.shadowDistance = Camera.main.transform.position.y * 2;
        //}
        //}
        if (Camera.main.transform.position.y < 10)
        {
            QualitySettings.shadowDistance = 30;
         
        }
        else
        {
            QualitySettings.shadowDistance = Camera.main.transform.position.y * 2;
        }
   
    }

    //public void BlurEffect()
    //{
    //    float dis = Vector3.Distance(transform.position , Vector3.zero);
        
    //    if (dis < 600)
    //    {
    //        DepthOfFieldModel depthOfField = this.profile.depthOfField;
    //        UnityEngine.PostProcessing.DepthOfFieldModel.Settings depthOfFieldSetting = new UnityEngine.PostProcessing.DepthOfFieldModel.Settings
    //        {
    //            focusDistance = Camera.main.transform.position.y * 0.1f + 30,
    //            //focusDistance = Camerdis,
    //            aperture = 4f,
    //            focalLength = 170,
    //            useCameraFov = false,
    //            kernelSize = DepthOfFieldModel.KernelSize.Medium
    //        };
    //        depthOfField.settings = depthOfFieldSetting;
    //    }
    //    else
    //    {
    //        DepthOfFieldModel depthOfField = this.profile.depthOfField;
    //        UnityEngine.PostProcessing.DepthOfFieldModel.Settings depthOfFieldSetting = new UnityEngine.PostProcessing.DepthOfFieldModel.Settings
    //        {
    //            focusDistance = 500,
    //            //focusDistance = Camerdis,
    //            aperture = 5f,
    //            focalLength = 195,
    //            useCameraFov = false,
    //            kernelSize = DepthOfFieldModel.KernelSize.Medium
    //        };
    //        depthOfField.settings = depthOfFieldSetting;
    //    }
     
    //}

    public void ColseFullScreen()
    {
        //Screen.SetResolution(1920, 1080, true);
        ////自己想要的分辨率，比如1024*768，true表示全屏 		
        //Screen.fullScreen = true;
    }



    /// <summary>
    /// 移动范围控制
    /// </summary>
   public void RangContrel()
    {

        if (CamerDemo.position.x > xMax)
        {
            CamerDemo.position = new Vector3(xMax, CamerDemo.position.y, CamerDemo.position.z);
        }
       if (CamerDemo.position.x < xMin)
        {
            CamerDemo.position = new Vector3(xMin, CamerDemo.position.y, CamerDemo.position.z);
        }
     if (CamerDemo.position.z < yMin)
        {
            CamerDemo.position = new Vector3(CamerDemo.position.x, CamerDemo.position.y, yMin);
        }
       if (CamerDemo.position.z > yMax)
        {
            CamerDemo.position = new Vector3(CamerDemo.position.x, CamerDemo.position.y, yMax);
        }
      if (CamerDemo.position.y < hMin)
        {
            
            CamerDemo.position = new Vector3(CamerDemo.position.x, hMin, CamerDemo.position.z);
        }
       if (CamerDemo.position.y > hMax)
        {
     
            CamerDemo.position = new Vector3(CamerDemo.position.x, hMax, CamerDemo.position.z);
        }
    }


}
